package com.qianxun.module.application.rest;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qianxun.module.application.entity.ApplicationDataRelation;
import com.qianxun.module.application.service.impl.BaseServiceImpl;
import com.qianxun.module.base.constant.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 关联数据前端处理器
 *
 * @author Admin
 */
@RequestMapping("/appData")
@RestController
public class ApplicationDataRelationController {

    @Resource
    private BaseServiceImpl baseService;

    @RequestMapping("/save")
    public Result save(String appId, String dataType, String dataKey, String sortMap) {
        if (StringUtils.isBlank(dataKey)) {
            return Result.fail("数据key不能为空");
        }

        ApplicationDataRelation relation = new ApplicationDataRelation();
        relation.setAppId(appId);
        relation.setDataType(dataType);
        relation.setDataKey(dataKey);
        long begin = System.currentTimeMillis();
        String s = baseService.dealSharingTable(relation.getAppId(), relation.getDataType(), relation.getDataKey(), sortMap);
        long end = System.currentTimeMillis();
        System.err.println("耗时----->>>>>>> " + (end - begin));
        if (StringUtils.isNotBlank(s)) {
            return Result.fail(s);
        }
        return Result.success();
    }

    @RequestMapping("/queryOne")
    public Result queryOne(String appId, String dataType, String dataKey) {
        if (StringUtils.isBlank(dataKey)) {
            return Result.fail("数据key不能为空");
        }
        List<ApplicationDataRelation> dataRelationList = baseService.queryDataPage(appId, dataType, dataKey, null, null);
        if (Objects.isNull(dataRelationList) || dataRelationList.size() == 0) {
            return Result.success(null, "暂无数据，请刷新重试");
        }
        return Result.success(dataRelationList.get(0));
    }

    @RequestMapping("/queryAll")
    public Result queryAll(String appId, String dataType, String dataKey, String sortMap) {
        List<ApplicationDataRelation> dataRelationList = baseService.queryDataPage(appId, dataType, dataKey, sortMap, null);
        if (Objects.isNull(dataRelationList) || dataRelationList.size() == 0) {
            return Result.success(null, "暂无数据，请刷新重试");
        }
        return Result.successList(dataRelationList);
    }


    @RequestMapping("/queryPage")
    public Result queryPage(String appId, String dataType, String dataKey, String sortMap,
                            @RequestParam(name = "current", defaultValue = "1", required = false) int current,
                            @RequestParam(name = "size", defaultValue = "10", required = false) int size) {
        Page<ApplicationDataRelation> page = new Page<>(current, size);
        List<ApplicationDataRelation> list = baseService.queryDataPage(appId, dataType, dataKey, sortMap, page);
        page.setRecords(list);
        return Result.successPage(page.getRecords(), page.getTotal(), page.getCurrent(), page.getSize());
    }

}
